package com.gt.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.admin.domain.EsCheckFlow;
import com.gt.app.common.ColonoscopyEnum;
import com.gt.app.domain.EsColonoscopyCheckSymptom;
import com.gt.app.domain.dto.EsCheckFlowDto;
import com.gt.app.mapper.EsCheckFlowMapper;
import com.gt.app.service.EsCheckFlowService;
import com.gt.app.service.EsColonoscopyCheckSymptomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 检查随访 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsCheckFlowServiceImpl extends ServiceImpl<EsCheckFlowMapper, EsCheckFlow> implements EsCheckFlowService {

    @Autowired
    private EsColonoscopyCheckSymptomService esColonoscopyCheckSymptomService;

    @Override
    public List<EsCheckFlowDto> getCheckFlow(String checkId) {
        List<EsCheckFlowDto> checkFlows = new ArrayList<>();
        LambdaQueryWrapper<EsCheckFlow> wrapper = new LambdaQueryWrapper<EsCheckFlow>().eq(EsCheckFlow::getEsCheckId, checkId);
        List<EsCheckFlow> checkFlowList = this.list(wrapper);
        if (checkFlowList == null){
            return null;
        }
        for (EsCheckFlow esCheckFlow : checkFlowList) {
            EsCheckFlowDto esCheckFlowDto = new EsCheckFlowDto();
            BeanUtil.copyProperties(esCheckFlow,esCheckFlowDto);

            LambdaQueryWrapper<EsColonoscopyCheckSymptom> wapper = new LambdaQueryWrapper<EsColonoscopyCheckSymptom>().eq(EsColonoscopyCheckSymptom::getCloOrdrowid, esCheckFlow.getId());
            List<EsColonoscopyCheckSymptom> list = esColonoscopyCheckSymptomService.list(wapper);

            List<String> lesionSiteItem = new ArrayList<>();
            List<String> pathologyLesionItem = new ArrayList<>();
            List<String> pathologyIncreaseItem = new ArrayList<>();

            for (EsColonoscopyCheckSymptom esColonoscopyCheckSymptom : list) {
                if (ColonoscopyEnum.LESION_SITE.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    lesionSiteItem.add(esColonoscopyCheckSymptom.getItem());
                }
                if (ColonoscopyEnum.PATHOLOGY_LESION.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    pathologyLesionItem.add(esColonoscopyCheckSymptom.getItem());
                }
                if (ColonoscopyEnum.PATHOLOGY_INCREASE.getName().equals(esColonoscopyCheckSymptom.getType())) {
                    pathologyIncreaseItem.add(esColonoscopyCheckSymptom.getItem());
                }
            }
            esCheckFlowDto.setLesionSiteItem(lesionSiteItem);
            esCheckFlowDto.setPathologyLesionItem(pathologyLesionItem);
            esCheckFlowDto.setPathologyIncreaseItem(pathologyIncreaseItem);
            checkFlows.add(esCheckFlowDto);
        }

        return checkFlows;
    }
}
